<template>
    <div class="el-input__inner_box">
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/index/home' }"
                >首页</el-breadcrumb-item
            >
            <el-breadcrumb-item
                ><a href="/index/users">用户管理</a></el-breadcrumb-item
            >
        </el-breadcrumb>
        <el-divider></el-divider>
        <el-input
            class="el-input__inner"
            v-model="input"
            placeholder="请输入内容"
        ></el-input>
        <el-button type="primary">搜索</el-button>
        <el-button
            icon="el-icon-circle-plus-outline"
            type="primary"
            @click="show = true"
            >添加
        </el-button>
        <el-table :data="tableData" style="width: 100%">
            <el-table-column class="table_head" label="用户编号" prop="_id">
            </el-table-column>
            <el-table-column class="table_head" label="用户名" prop="account">
            </el-table-column>
            <el-table-column
                class="table_head"
                label="真实姓名"
                prop="realname"
            >
            </el-table-column>
            <el-table-column
                class="table_head"
                label="联系方式"
                prop="telephone"
            >
            </el-table-column>
            <el-table-column class="table_head" label="操作" align="center">
                <template slot-scope="scope">
                    <el-button
                        icon="el-icon-delete"
                        size="mini"
                        type="danger"
                        @click="delAdmins(scope.row._id)"
                        >删除</el-button
                    >
                </template>
            </el-table-column>
        </el-table>
        <div class="page">
            <el-pagination
                :current-page.sync="currentPage"
                :page-sizes="[10, 20, 30, 40]"
                :page-size="10"
                layout="prev, pager, next,sizes"
                :total="tableData.length"
            >
            </el-pagination>
        </div>
        <el-dialog title="添加" :visible.sync="show" width="500px">
            <el-form :model="form">
                <el-form-item label="用户名" label-width="80px">
                    <el-input v-model="form.account"></el-input>
                </el-form-item>
                <el-form-item label="密码" label-width="80px">
                    <el-input
                        v-model="form.password"
                        type="password"
                    ></el-input>
                </el-form-item>
                <el-form-item label="真实姓名" label-width="80px">
                    <el-input v-model="form.realname"></el-input>
                </el-form-item>
                <el-form-item label="手机号" label-width="80px">
                    <el-input v-model="form.telephone"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="editShow = false">取 消</el-button>
                <el-button type="primary" @click="addAdmins">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
export default {
    data() {
        return {
            show: false,
            input: "",
            currentPage: 0,
            tableData: [],
            form: {},
        };
    },
    methods: {
        changeShow() {
            this.show = !this.show;
        },
        handleEdit(index, row) {
            console.log(index, row);
            // .filter(data => !search || data.name.toLowerCase().includes(search.toLowerCase()))
        },
        async delAdmins(_id) {
            const res = await this.$api.users.delAdmins({ _id });
            console.log(res);
            if (res.code == 200) {
                this.$message({
                    message: res.msg,
                    type: "success",
                });
                this.getUsers();
            }
        },
        async getUsers() {
            const res = await this.$api.users.getUsers();
            if (res.code == 200) {
                this.tableData = res.message;
            }
        },
        async addAdmins() {
            const res = await this.$api.users.addAdmins(this.form);
            if (res.code == 200) {
                this.$message({
                    message: res.msg,
                    type: "success",
                });
                this.show = false;
                this.form = {};
                this.getUsers();
            }
        },
    },
    computed: {},
    watch: {
        tableData: {
            handler() {
                // this.getUsers();
                // this.changeShow()
            },
            deep: true,
        },
    },
    created() {
        this.getUsers();
    },
};
</script>

<style scoped>
.el-input__inner >>> {
    width: 277px;
    padding: 0;
    margin-bottom: 20px;
}
.table_head {
    background-color: rgb(250, 250, 250);
}
.page {
    display: flex;
    justify-content: flex-end;
}
</style>